<?php
/**
 * Created by PhpStorm.
 * User: openoter
 * Date: 2018/12/11
 * Time: 23:53
 */

namespace app\admin\controller;


use app\admin\model\Attendance;
use app\admin\model\Cadre;
use think\Request;

class AttendanceController extends BaseController {

    public function index(Request $request) {
//        $attendance =  Attendance::with('cadre')->paginate(10);
        $kw = $request->param('kw', '');
        $time = $request->param('time', '');

        $mktime = strtotime($time);
        $start_time = mktime(0, 0, 0, date('m',$mktime ), date('d', $mktime), date('Y', $mktime));
        $end_time = mktime(23, 59, 59, date('m',$mktime ), date('d', $mktime), date('Y', $mktime));
        $whereAMobile = [];
        $whereAUsername = [];
        $whereAUser = [];
        $whereMobile = [];
        $whereUsername = [];
        $whereUser = [];
        $where = [];
        if ($kw) {
            // 手机搜索
            $whereMobile['mobile'] = ['=', $kw];
            // 用户名
            $whereUser['name'] = ['like', "%{$kw}%"];
            // 用户名
            $whereUsername['username'] = ['like', "%{$kw}%"];

//            mktime(0, 0, 0, date('m'), date('d'), date('Y')),
//            mktime(23, 59, 59, date('m'), date('d'), date('Y'))
            if ($time) {
                $whereAUser['addtime'] = $whereAUsername['addtime'] = $whereAMobile['addtime'] = ['between', [$start_time, $end_time]];;
            }
        } else {
            if ($time) {
                $where['addtime'] = ['between', [$start_time, $end_time]];
            }
        }
        $cadre = Cadre::whereOr($whereMobile)
            -> whereOr($whereUser)
            -> whereOr($whereUsername)
            ->with(['attendance'=>function($query) use ($whereAUser, $whereAUsername, $whereAMobile){
                $query->group('type')
                    -> whereOr($whereAUser)
                    -> whereOr($whereAUsername)
                    -> whereOr($whereAMobile)
                    -> order('addtime', 'desc')
                    -> field(['type', 'uid','addtime', 'count(type) as type_count']);
            }, 'cls'=>function($query) {
                $query->field(['name', 'id']);
            }])->paginate(10);

        foreach ($cadre as $key=>$item) {
            $attendance = $item['attendance'];
            foreach ($attendance as $val ) {
                switch ($val['type']) {
                    case 1:
                        $cadre[$key]['normal'] = $val['type_count'];
                        break;
                    case 2:
                        $cadre[$key]['late'] = $val['type_count'];
                        break;
                    case 3:
                        $cadre[$key]['leave_early'] = $val['type_count'];
                        break;
                }
            }
            if ($attendance) {
                $cadre[$key]['last_time'] = $attendance[0]['addtime'];
            } else {
                $cadre[$key]['normal'] = 0;
                $cadre[$key]['late'] = 0;
                $cadre[$key]['leave_early'] = 0;
                $cadre[$key]['last_time'] = 0;
            }
            unset($cadre[$key]['attendance']);
        }

//        print_r(collection($cadre)->toArray());
        $this->assign('date', date('Y-m-d'));
        $this->assign('cadre', $cadre);
        return view('index');
    }

    /**
     * 详情
     * @param $id
     */
    public function detail(Request $request, $id) {

        $dateStr = $request->param('date', '');
//        $uid= $request->param('uid', '');
        $type= $request->param('type', '-1');
        $where['uid'] = ['=',$id];
        if (!$dateStr) {
            $dateStr = date('Y-m-d');
        }
        $date = strtotime($dateStr);
        $start = mktime(0, 0, 0, date('m', $date), 1, date('Y', $date));
        $end = mktime(23, 59, 59, date('m', $date), date('t', $date), date('Y', $date));
        $where['addtime'] = ['between', [$start, $end]];
        if ($type != -1) {
            $where['type'] = ['=', $type];
        }
        $a = Attendance::where($where)->select();

        $d = [];
        foreach ($a as $v) {
            $time['type'] = intval($v['type']);
            $time['signDay'] = intval(date('d', $v['addtime']));
            $time['id'] = $v['id'];
            array_push($d, $time);
        }
        $this->assign('date', $dateStr);
        $this->assign('d', json_encode($d));
        return view('detail');
    }

    /**
     * 获取签到数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSign(Request $request) {
        $param = $request->param();
        $validate = $this->validate($param, [
            'uid' => 'require',
            'date' => 'require',
        ]);

        if ($validate !== true) {
            $data = [
                'message' => $validate,
                'status' => 404
            ];
            return json($data);
        }

        $date = strtotime($param['date']);
        $start = mktime(0, 0, 0, date('m', $date), 1, date('Y', $date));
        $end = mktime(23, 59, 59, date('m', $date), date('t', $date), date('Y', $date));
        $a = Attendance::where(['uid'=>$param['uid'], 'addtime'=>['between', [$start, $end]]])->select();

        $d = [];
        foreach ($a as $v) {
            $time['type'] = intval($v['type']);
            $time['signDay'] = intval(date('d', $v['addtime']));
            $time['id'] = $v['id'];
            array_push($d, $time);
        }
        $data = [
            'data' => $d,
            'status' => 200
        ];
        return json($data);
    }

    /**
     * 获取内容
     * @param $id
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function getContent($id) {
        $a = Attendance::get($id);
        if ($a){
            $a['addtime'] = date('Y-m-d H:m:s', $a['addtime']);
            $data = [
                'data' => $a,
                'status' => 200
            ];
        } else {
            $data = [
                'message' => "error",
                'status' => 404
            ];
        }

        return json($data);
    }
}